<template>
	<view>
		<view class="container" v-if="list.length">
			<view class="s-edit-goods">
				<view>购物车共<text class="s-goods-num">{{num_all}}</text>件商品</view>
				<view>
					<s-button type="gray" :plain="true" shape="circle" size="mini" @click="click_status">{{is_status?'编辑商品':'完成'}}</s-button>
				</view>
			</view>
			<view class="s-cart-cell  s-mtop">
				<view class="s-goods-item" v-for="(item,index) in list" :key="index">
					<view class="s-checkbox" @tap="set_cart_status" :data-index="index">
						<view class="checkbox">
							<view :class="item.status?'on':''"></view>
						</view>
					</view>
					<image :src="item.image_url" class="s-goods-img" />
					<view class="s-goods-info">
						<view class="s-goods-title">
							{{item.name}}
						</view>
						<view class="s-goods-model">
							<view class="s-model-text">{{item.specs}}</view>
						</view>
						<view class="s-price-box">
							<view class="s-goods-price">￥{{item.money}}</view>
							<view class="s-scale">
								<s-numberbox :value="item.num" :max="item.stock" :height="40" :width="74" :min="1" :index="index" @change="changeNum"></s-numberbox>
							</view>
						</view>
					</view>
				</view>
			</view>
			
			<!--tabbar-->
			<view class="s-tabbar">
				<view class="s-checkAll">
					<label class="s-checkbox" @tap="set_cart_status_all">
						<view class="checkbox">
							<view :class="status_all?'on':''"></view>
						</view>
						<text class="s-checkbox-pl">全选</text>
					</label>
					<view class="s-total-price" v-if="is_status">合计:
						<text class="s-bold">￥{{money_all}}</text>
					</view>
				</view>
				<view>
					<s-button size="mini" v-if="is_status" type="danger" shape="circle" @click="cart_submit_order">去结算({{num_all_m}})</s-button>
					<s-button size="mini" v-else type="danger" shape="circle" :plain="true" @click="cart_delete">删除</s-button>
				</view>
			</view>
		</view>
		<s-no-data v-if="list.length<=0&&isData===true" :fixed="true" :imgUrl="$s.config.getConfig('NoDataImgUrlA')" btnText="去逛逛" @click="product_list">
			{{$s.config.getConfig('NoDataTitle')}}
		</s-no-data>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isData:false,
				btn_type:1,
				is_status:true,
				money_all:'0.00',
				num_all:0,
				status_all:0,
				num_all_m:0,
				list:[]
			}
		},
		async onShow() {
			await this._onLoad()
			await this.$s.model.cart.getCartSum()
		}, 
		
		methods: {
			async _onLoad(){
				const data = await this.$s.model.cart.getCartList()
				this.list = data.list || []
				this.money_all = data.money_all
				this.num_all = data.num_all
				this.status_all = data.status_all
				this.num_all_m = data.num_all_m
				this.isData = true
			},
			//跳转提交订单页
			async cart_submit_order(){
				var i = 0;
				for(var j = 0; j<this.list.length;j++){
					if(this.list[j].status){
						i++;
					}
				}
				if(i==0){
					return await this.$s.uniApi.showToast({title:'请选择商品！'});
				}
				return await this.$s.uniApi.navigateTo({url:'../cart_submit_order/cart_submit_order'})
			},
			//删除切换
			click_status(){
				this.is_status = !this.is_status
			},
			//删除
			async cart_delete(){
				const res = await this.$s.uniApi.showModal({content:'确定要删除选中商品吗？'})
				if (res.confirm) {
					await this.$s.model.cart.cartDelete()
					await this._onLoad()
				}
			},
			// 单选
			async set_cart_status(e){
				const index = this.$s.api.getDataSet(e,'index');
				const data = await this.$s.model.cart.setCartStatus({id:this.list[index].id})
				this.money_all = data.money_all
				this.num_all = data.num_all
				this.num_all_m = data.num_all_m
				this.status_all = data.status_all
				this.list[index].status = data.status
			},
			// 全选全不选
			async set_cart_status_all(){
				const data = await this.$s.model.cart.setCartStatusAll()
				this.money_all = data.money_all
				this.num_all = data.num_all
				this.num_all_m = data.num_all_m
				this.status_all = data.status_all
				for(var i = 0; i<this.list.length;i++){
					this.list[i].status = data.status_all
				}
			},
			//购物车减或加
			async changeNum(e){
				const data = await this.$s.model.cart.setCartSubtract({
					id:this.list[e.index].id,
					num:e.value
				})
				this.money_all = data.money_all
				this.num_all = data.num_all
				this.num_all_m = data.num_all_m
				this.status_all = data.status_all
				this.list[e.index].status = 1
				this.list[e.index].num = e.value
				await this.$s.model.cart.getCartSum()
			},
			// 跳转去商品列表
			async product_list(){
				return await this.$s.uniApi.navigateTo({url:'../product_list/product_list'})
			}
		}
	}
</script>

<style lang="scss" scoped>
@import 'cart.scss';
</style>